﻿using System;
using System.Collections.Specialized;
using System.Reflection;

namespace VitCode.Castle.Helper
{
    public class ObjectBuildHelper
    {
        

        public object InvokeObjMethod(object obj, string methodName, object[] parms)
        {
            object resultVal = null;
            MethodInfo m = obj.GetType().GetMethod(methodName);
            if (m != null)
            {
                object moResultVal = m.Invoke(obj, parms);
                if (moResultVal != null)
                    resultVal = moResultVal;
            }
            return resultVal;
        }

        public string GetObjProStringValue(PropertyInfo pro, object obj)
        {
            string strVal = "";
            object proVal = pro.GetValue(obj, null);
            if (proVal == null)
            {
                if (pro.PropertyType == typeof (Guid))
                    strVal = Guid.Empty.ToString();
                else if (pro.PropertyType.IsEnum || pro.PropertyType == typeof (Int16) ||
                         pro.PropertyType == typeof (Int16) || pro.PropertyType == typeof (Double) ||
                         pro.PropertyType == typeof (Int32) || pro.PropertyType == typeof (Int64)
                         || pro.PropertyType == typeof (Single))
                    strVal = "0";
            }
            else
                strVal = proVal.ToString();
            return strVal;
        }

        public string GetProObjStringValue(string proName, object obj)
        {
            if (obj == null)
                return "";
            PropertyInfo pi = obj.GetType().GetProperty(proName);
            if (pi == null)
                return "";
            return GetObjProStringValue(pi, obj);
        }

        public string GetStringValue(Type valType, object valObject)
        {
            string strVal = "";
            if (valObject == null)
            {
                if (valType == typeof(Guid))
                    strVal = Guid.Empty.ToString();
                else if (valType.IsEnum || valType == typeof(Int16) || valType == typeof(Int16) || valType == typeof(Double) || valType == typeof(Int32) || valType == typeof(Int64)
                         || valType == typeof(Single))
                    strVal = "0";
            }
            else
                strVal = valObject.ToString();
            return strVal;
        }

        public void SetProperty(object o, string proName, object proValue)
        {
            if (o == null)
                return;
            PropertyInfo pi = o.GetType().GetProperty(proName);
            if (pi == null)
                return;
            pi.SetValue(o, proValue, null);

        }

        public PropertyInfo GetProperty(string proName, object obj)
        {
            Type type = obj.GetType();
            PropertyInfo pi = type.GetProperty(proName);
            return pi;
        }

        public object GetPropertyValue(string proName, object obj)
        {
            if (obj == null)
                throw new ArgumentException("等待設置的物件不能這空");
            PropertyInfo p = GetProperty(proName, obj);
            return p.GetValue(obj, null);
        }
    }
}
